<?php

namespace app\common\command;

use app\common\enums\AppointmentStatusEnum;
use app\common\model\banxi\core\CoreCourseAppointment;
use app\common\service\ConfigService;
use app\common\service\course\CourseAppointmentService;
use think\console\Command;
use think\console\Input;
use think\console\Output;

/**
 * 配置
 */
class KaiKeSendMsg extends Command
{
    protected function configure()
    {
        $this->setName('KaiKeSendMsg')
            // ->addArgument('name', Argument::OPTIONAL, "your name")
            // ->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
            ->setDescription('send course appointment task subscribe message');
    }

    protected function execute(Input $input, Output $output)
    {
        $output->writeln("开课提醒定时任务开始");
        $this->doCommand();
        $output->writeln("开课提醒定时任务结束");
    }

    protected function doCommand()
    {
        $configService = new ConfigService();
        $courseStartRemindHours = $configService->getSysConfigValue('course_start_remind_hours');

        $date = date('Y-m-d');
        $currentTime = time();
        $futureTime = strtotime('+' . $courseStartRemindHours . ' hours', $currentTime);
        $time = date('H:i:00', $futureTime);
        // 比较日期而不是仅仅判断小时是否为23
        $futureDate = date('Y-m-d', $futureTime);
        if ($futureDate !== $date) {
            $date = $futureDate;
        }
        $coreCourseAppointmentModel = new CoreCourseAppointment();
        $ids = $coreCourseAppointmentModel->where([
            'date' => $date,
            'startTime' => $time,
            'appointmentStatus' => AppointmentStatusEnum::PENDING_CLASS,
            'isKaiKeMsgSend' => 0
        ])->column('id');
        foreach ($ids as $id) {
            $courseAppointmentService = new CourseAppointmentService();
            $courseAppointmentService->sendKaiKeMsg($id);
        }
        return true;
    }

}